package com.atguigu.order.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.order.bean.Order;
import com.atguigu.order.feign.OrderFeignClient;
import com.atguigu.order.service.OrderService;
import com.atguigu.product.bean.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    DiscoveryClient discoveryClient;
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    OrderFeignClient orderFeignClient;
    @SentinelResource(value = "createOrder" ,blockHandler = "createOrderFallback")
    @Override
    public Order createOrder(Long productId,Long userId) {
//        Product product = getProductFromRemoteWithLoadBalanceAnnotation(productId);
        //使用feign完成商品信息获取
        Product product = orderFeignClient.getProductById(productId);
        Order order = new Order();
        order.setId(1L);
        // Todo: 获取商品信息
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUsreId(userId);
        order.setNickName("张三");
        order.setAddress("北京");
        // Todo: 获取商品列表
        order.setProductList(Arrays.asList(product));
        return order;
    }
    // 降级处理
    public Order createOrderFallback(Long productId, Long userId, BlockException e) {
//        Product product = getProductFromRemoteWithLoadBalanceAnnotation(productId);
        //使用feign完成商品信息获取
        Order order = new Order();
        order.setId(1L);
        // Todo: 获取商品信息
        order.setTotalAmount(new BigDecimal(0));
        order.setUsreId(userId);
        order.setNickName("未知用户");
        order.setAddress("异常信息："+e.getClass());
        // Todo: 获取商品列表
        order.setProductList(null);
        return order;
    }

    public Product getProductFromRemote(Long productId) {
        // Todo: 调用商品服务获取商品信息

        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);
        String ip = instance.getUri().toString();
        log.info("ip:{}",ip);
        Integer port = instance.getPort();
        // http://127.0.0.1:9000/product/4
        String url = ip + "/product/" + productId;
        log.info("url:{}",url);
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
    // 进阶：完成负载均衡发送请求
    public Product getProductFromRemoteWithLoadBalance(Long productId) {
        // Todo: 调用商品服务获取商品信息

//        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = loadBalancerClient.choose("service-product");
        String ip = instance.getUri().toString();
        log.info("ip:{}",ip);
        Integer port = instance.getPort();
        // http://127.0.0.1:9000/product/4
        String url = ip + "/product/" + productId;
        log.info("url:{}",url);
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    // 进阶：基于注解负载均衡发送请求
    public Product getProductFromRemoteWithLoadBalanceAnnotation(Long productId) {
        // Todo: 调用商品服务获取商品信息
        // http://127.0.0.1:9000/product/4
        String url =  "http://service-product/product/" + productId;
        log.info("url:{}",url);
        //给远程发送请求 service-product 动态替换
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
}
